﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    private float cellSize = 30f;       //格子的宽度
    private const int width = 10;       //10行
    private const int height = 10;      //10列
    private GameObject cellPrefab;      //格子预设体
    private Transform[,] maps;      //格子地图的引用
    private Vector3 startPos;       //起始位置
    private bool[,] canSet;     //格子是否可用

    private static GameManager instance;

    public static GameManager GetInstance()
    {
        return instance;
    }

    void Awake()
    {
        instance = this;
    }
	// Use this for initialization
	void Start ()
	{
	    Init();
	    SetCell(0, 0, new bool[,]
	    {
	        {
	            true, true,false,true
	        },
            {
	            true, false,false,true
	        }
	    }, Color.red);
	}

    void Update2()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 pos = Input.mousePosition;
            int y = Mathf.RoundToInt((pos.x - startPos.x) / cellSize);
            int x = Mathf.RoundToInt((pos.y - startPos.y) / cellSize);
            print("index = " + x + " : " + y);
        }
    }

    public void Init()
    {
        cellPrefab = Resources.Load("cell") as GameObject;
        maps = new Transform[width, height];
        canSet = new bool[width, height];

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                GameObject cell = Instantiate(cellPrefab);
                cell.transform.SetParent(transform);
                maps[i, j] = cell.transform;        //获取引用
                cell.name = i.ToString() + " " + j.ToString();
                canSet[i, j] = true;
            }
        }
        StartCoroutine(SetStartPos());
    }

    IEnumerator SetStartPos()
    {
        yield return 0;
        startPos = maps[0, 0].position;
        print(startPos);
    }

    /// <summary>
    /// Set The Cell
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="body"></param>
    /// <param name="color"></param>
    private void SetCell(int x, int y, bool[,] body, Color color)
    {
        //遍历格子形状
        for (int i = 0; i < body.GetLength(0); i++)
        {
            for (int j = 0; j < body.GetLength(1); j++)
            {
                if (body[i, j]) //
                {
                    GameObject cell = (GameObject) Instantiate(cellPrefab);
                    cell.transform.SetParent(maps[x + i, y + j]);
                    cell.GetComponent<Image>().color = color;
                    cell.GetComponent<RectTransform>().sizeDelta = new Vector2(cellSize, cellSize);
                    cell.transform.localScale = Vector3.one;
                    cell.transform.localPosition = Vector3.zero;
                    canSet[x + i, y + j] = false;
                }
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="pos"></param>
    /// <param name="body"></param>
    /// <param name="color"></param>
    public void Check(Vector3 pos, bool[,] body, Color color)
    {
        int y = Mathf.RoundToInt((pos.x - startPos.x) / cellSize);
        int x = Mathf.RoundToInt((startPos.y - pos.y) / cellSize);
        print("index = " + x + " : " + y);
        if (x >= 0 && x < width && y >= 0 && y < height)
        {
            for (int i = 0; i < body.GetLength(0); i++)
            {
                for (int j = 0; j < body.GetLength(1); j++)
                {
                    if (body[i, j])
                    {
                        int tempx = x + i;
                        int tempy = y + j;
                        if(tempx < 0 || tempy < 0 || tempx >= width || tempy>= height || !canSet[tempx,tempy])
                            return;
                    }
                }
            }
            SetCell(x, y, body, color);
        }
    }
}
